package mock.action;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import mock.bo.ViewHistory;
import mock.bo.ViewHistoryImp;
import mock.dto.LogTransferDTO;
import mock.dto.LogViewDTO;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ViewHistoryAction extends ActionSupport {

	private ViewHistory viewHistoryBO = new ViewHistoryImp();

	private Map<String, Object> session = ActionContext.getContext()
			.getSession();

	private String btnAction;
	private int currentPage;
	private String timeAgo;
	private int maxPage;
	private List<LogViewDTO> viewList;

	public List<LogViewDTO> getViewList() {
		return viewList;
	}

	public void setViewList(List<LogViewDTO> viewList) {
		this.viewList = viewList;
	}

	public int getMaxPage() {
		return maxPage;
	}

	public void setMaxPage(int maxPage) {
		this.maxPage = maxPage;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public String getTimeAgo() {
		return timeAgo;
	}

	public void setTimeAgo(String timeAgo) {
		this.timeAgo = timeAgo;
	}

	public String getBtnAction() {
		return btnAction;
	}

	public void setBtnAction(String btnAction) {
		this.btnAction = btnAction;
	}

	/**
	 * dat gia tri cho timeAgo thong qua cac button
	 */
	private void setTimeAgo() {
		switch (btnAction) {
		case "button1":
			setTimeAgo("1w");
			break;
		case "button2":
			setTimeAgo("1m");
			break;
		case "button3":
			setTimeAgo("4m");
			break;
		case "button4":
			setTimeAgo("6m");
			break;
		case "button5":
			setTimeAgo("1y");
			break;
		case "button6":
			setTimeAgo("2y");
			break;
		}
	}

	/**
	 * thuc hien lay ket qua trong database roi view len trang view history
	 */
	public String loadPage() throws Exception {
		// TODO Auto-generated method stub
		// lay session cardNo va atmid
		String cardNo = (String) session.get("cardNo");
		int atmID = (int) session.get("ATMID");
		// set gia tri time ago
		setTimeAgo();
		session.put("timeAgo", timeAgo);
		// set gia tri current page
		currentPage = 1;
		// set gia tri maxPage
		maxPage = viewHistoryBO.getMaxPage(cardNo, timeAgo);
		if(maxPage == -1){
			maxPage = 1;
			return "error";
		}
		viewList = viewHistoryBO.getLogViewByATMIDAndCardNoAndTimeAgo(cardNo,
				timeAgo, currentPage);
		if (viewList == null)
			return "error";
		return "success";
	}

	/**
	 * thay doi giua cac trang roi dua len view
	 */
	public String changePage() {
		// lay session cardNo va atmid, timeAgo
		String cardNo = (String) session.get("cardNo");
		String timeAgo = (String) session.get("timeAgo");
		maxPage = viewHistoryBO.getMaxPage(cardNo, timeAgo);
		if(maxPage == -1){
			maxPage = 1;
			return "error";
		}
		if(btnAction.equals("button6")){ 
			currentPage = this.currentPage + 1;
			if(currentPage > maxPage) currentPage = currentPage - 1;
		}
		else if(btnAction.equals("button3")){ 
			currentPage = this.currentPage - 1;
			if(currentPage <= 0) currentPage = currentPage + 1;
		}
		viewList = viewHistoryBO.getLogViewByATMIDAndCardNoAndTimeAgo(cardNo,
				timeAgo, currentPage);
		if (viewList == null)
			return "error";
		return "success";
	}

}
